package com.ruike.utils;

import org.springframework.util.DigestUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

/**
 * @author 吴泽胜
 * @FileName MD5Utils
 * @date 2020-11-12 2:03 下午
 * @Software: IntelliJ IDEA
 */

/**
 * 加密MD5密码工具类
 */
public class MD5Utils {

    // 盐
    private static final String solt = "ruike/**-123.-+";

    /**
     * @param string
     * @return
     */
    public static String getMD5(String string){
        String val = string + solt;
        return DigestUtils.md5DigestAsHex(val.getBytes());
    }

    /**
     * @param ch
     * @return
     */
    public static String getMD5(char[] ch){
        return DigestUtils.md5DigestAsHex(String.valueOf(ch).getBytes());
    }

    public static String getSolt(){
        return solt;
    }

    public static void test(){
        Object credentials = "123456";
        Object salt = ByteSource.Util.bytes( credentials + MD5Utils.getSolt() );;
        int hashIterations = 1024;
        SimpleHash simpleHash = new SimpleHash("md5",credentials, ByteSource.Util.bytes(salt),hashIterations);
        System.out.println(simpleHash);
    }

    /**
     *  test
     * @param args
     */
    public static void main(String[] args) {
        String md5 = new MD5Utils().getMD5("123456");
        System.out.println(md5 + "    " + md5.length());
        test();
    }


}
